//
//  LoginBaseController.m
//  FinancialStore
//
//  Created by 王晓萌 on 16/9/8.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "LoginBaseController.h"
#import "LoginMainView.h"
#import "LoginMainAction.h"
//#import "CreditKoalaPointsViewController.h"
#import "Common.h"
#import "NSString+HLQRegex.h"

static const NSInteger timer_sec = 90;
@interface LoginBaseController () <UITableViewDataSource,UITableViewDelegate>


@end

@implementation LoginBaseController
@synthesize loginMainView;
@synthesize loginMainAction;
@synthesize timer;
@synthesize configureStyle;
@synthesize goBack;
@synthesize timer_count;
@synthesize isEditing;

-(instancetype)init{
    if(self = [super init]){
        
        loginMainView = nil;
        loginMainAction = [[LoginMainAction alloc] init];
        timer = nil;
        configureStyle = kLocalConfigLogin;
        goBack = 0;
        timer_count = timer_sec;
        isEditing = NO;
    }
    return self;
}

-(void)loadView{
    [super loadView];
    loginMainView = [[LoginMainView alloc] init];
    self.view = self.loginMainView;
    
    __weak typeof(self) weakself = self;
    self.loginMainView.commonJump = ^(NSInteger index){
        [LoginMainAction jumpWith:weakself index:index andIsRn:weakself.isRn];
    };
    
    self.loginMainView.dismissCurrent = ^(){
        GYNLog(@"CreditKoalaPointsViewController");
//        CreditKoalaPointsViewController *credit = [[CreditKoalaPointsViewController alloc]init];
//        credit.from = 1;
//        credit.url = [NSString stringWithFormat:@"%@%@",HaoDai_URL,@"/home/rongba/agreement.html"];
//        [weakself.navigationController pushViewController:credit animated:YES];
    };
    
    
}

- (void)viewDidLoad{
    [super viewDidLoad];
    UITapGestureRecognizer * tGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    [self.view addGestureRecognizer:tGestureRecognizer];
    [self.loginMainView.mainEventBtn addTarget:self action:@selector(loginBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginMainView.coreView.dataSource = self;
    self.loginMainView.coreView.delegate = self;


}

-(void)dealloc{
    
}

#pragma mark -
#pragma mark tableview datasource 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 0;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return nil;
}


#pragma mark -
#pragma mark  tableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return LOGIN_NORMAL_CELL_HEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 13.f;
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    view.tintColor = [UIColor clearColor];
}


#pragma mark -
#pragma mark textfiled delegate 
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    return YES;
}



-(void)verifyBtnPressed:(id)sender{
    
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    NSString * phone = cell.userInput.text;
    if (phone.length <= 0) {
        [self showToastAddWithTitle:@"请输入手机号"];
        return;
    }
    
    if (![NSString validateMobile:phone]) {
        [self showToastAddWithTitle:@"请输入正确的手机号"];
        return;
    }
    
    
    [LoadingAnimalView showWhiteLoadingAddedTo:self.view];
    [self.loginMainAction getVerifyCode:phone withCallBack:^(id error_msg) {
        [LoadingAnimalView hideLoadingForView:self.view];
        if (error_msg) {
            [self showToastAddWithTitle:error_msg];
        }else {
            [self showToastAddWithTitle:@"验证码已发送"];
            timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerWithAction) userInfo:nil repeats:YES];
        }
    }];
    
}

-(void)timerWithAction {
    timer_count--;
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    LoginNormalCell * cell = (LoginNormalCell*)[[self.loginMainView coreView] cellForRowAtIndexPath:indexPath];
    [cell.getVerify setTitle:[NSString stringWithFormat:@"%lds",(long)timer_count] forState:UIControlStateNormal];
    [cell.getVerify setEnabled:NO];
    if (timer_count == 0) {
        [cell.getVerify setTitle:@"获取验证码" forState:UIControlStateNormal];
        [cell.getVerify setEnabled:YES];
        
        [self.timer invalidate];
        timer_count = timer_sec;
    }
}

-(void)hideKeyboard{
    
    [self.view endEditing:YES];
}

- (void)setIsRn:(BOOL)isRn{
  _isRn = isRn;
  
  if (!isRn) {
    return;
  }
  UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [self.view addSubview:leftButton];
  
  
  [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(0);
    make.top.equalTo(self.view).offset(15);
    make.width.height.mas_equalTo(44);
  }];
  [leftButton setImage:[UIImage imageNamed:@"back_icon" ] forState:UIControlStateNormal];
  [leftButton addTarget:self action:@selector(backPressed:)forControlEvents:UIControlEventTouchUpInside];
  
}

- (void)backPressed:(UIButton *)sender{
//  AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//  [delegate.preController.navigationController popViewControllerAnimated:YES];
    UIApplication *application = [UIApplication sharedApplication];
    UIWindow *window = application.keyWindow;
    window.backgroundColor = [UIColor whiteColor];
    [window.rootViewController.navigationController popViewControllerAnimated:YES];
    
  
}



@end
